package ca.on.eho.qa.applogic1;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import org.testng.Assert;
import static org.junit.Assert.*;

import ca.on.eho.qa.model.Client;
import ca.on.eho.qa.applogic.ClientHelper;
import ca.on.eho.qa.applogic1.ApplicationManager1;
import ca.on.eho.qa.applogic1.DriverBasedHelper;
import ca.on.eho.qa.pages.CSPortlet;
import ca.on.eho.qa.pages.CSLabResultsPage;
import ca.on.eho.qa.util.Constants;

public class ClientHelper1 extends DriverBasedHelper implements ClientHelper {

	private ApplicationManager1 manager;

	public ClientHelper1(ApplicationManager1 manager) {
		super(manager.getWebDriver());
		this.manager = manager;
	}

	@Override
	public void search(Client client) {

		pages.csPortlet.ensurePageLoaded();
		pages.csPortlet.clickClearButton();
		pages.csPortlet.setHealthNumberField(client.getHealthNumber());
		pages.csPortlet.setDOBField(client.getDOB());
		pages.csPortlet.setGenderDropdown(client.getGender());
		// CSPortlet.genderDropdown(driver).sendKeys(client.getGender());
		pages.csPortlet.clickSubmitButton();
		
		if (isElementLoaded(pages.csPortlet.by_confirmClientButton, Constants.MEDIUM_WAIT)) {
			pages.csPortlet.clickConfirmClientButton();
			client.setRegistered(true);
		} else {
			if (getErrors() != "") {
			System.out.println("Errors found: " + getErrors());
			}
			client.setRegistered(false);
		}

	}

	public String getErrors() {

		String errorText;
		errorText = pages.csPortlet.getHNError();
		errorText = errorText + pages.csPortlet.getDOBError();
		errorText = errorText + pages.csPortlet.getGenderError();
		errorText = errorText + pages.csPortlet.getNoPatientFound();
		return errorText;
	}

	@Override
	public void clear(Client client) {
		pages.csPortlet.clickClearButton();
	}

	@Override
	public boolean isClear() {
		assertEquals(pages.csPortlet.getHealthNumberField(), "");
		assertEquals(pages.csPortlet.getDOBField(), "");
		assertEquals(pages.csPortlet.getGenderDropdown(), "Select");
		return true;
	}

	@Override
	public boolean isHNInvalid() {
		assertEquals(pages.csPortlet.getHNError(), pages.csPortlet.HN_INVALID);
		return true;
	}
	
	@Override
	public boolean isHNRequired() {
		assertEquals(pages.csPortlet.getHNError(), pages.csPortlet.HN_REQUIRED);
		return true;
	}
	
	@Override
	public boolean isDOBInvalid() {
		assertEquals(pages.csPortlet.getDOBError(), pages.csPortlet.DOB_INVALID);
		return true;
	}
	
	@Override
	public boolean isDOBRequired() {
		assertEquals(pages.csPortlet.getDOBError(), pages.csPortlet.DOB_REQUIRED);
		return true;
	}
	
	@Override
	public boolean isGenderRequired() {
		assertEquals(pages.csPortlet.getGenderError(), pages.csPortlet.GENDER_REQUIRED);
		return true;
	}
	
	public boolean isNoPatientFound() {
		assertEquals(pages.csPortlet.getNoPatientFound(), pages.csPortlet.NO_PATIENT_FOUND);
		return true;
	}
	
	@Override
	public boolean isAgeCorrect(Client client) {
		assertEquals(pages.csPortlet.getAge(), client.getAge());
		return true;
	}

	@Override
	public boolean isInfoDisplayed(Client client) {
		assertNotEquals(pages.csPortlet.getInfo(), "");
		return true;
	}

	@Override
	public boolean isPortletElementsPresent() {
		Boolean bTempResult = true;

		if (!isElementLoaded(pages.csPortlet.by_healthNumberField, Constants.MEDIUM_WAIT)) {
			System.out.println("Health Number element not found.");
			bTempResult = false;
		}
		if (!isElementPresent(pages.csPortlet.by_dobField)) {
			System.out.println("DOB element not found.");
			bTempResult = false;
		}
		if (!isElementPresent(pages.csPortlet.by_genderDropdown)) {
			System.out.println("Gender element not found.");
			bTempResult = false;
		}
		if (!isElementPresent(pages.csPortlet.by_submitButton)) {
			System.out.println("Submit element not found.");
			bTempResult = false;
		}
		if (!isElementPresent(pages.csPortlet.by_clearButton)) {
			System.out.println("ClearS element not found.");
			bTempResult = false;
		}

		if (bTempResult == true) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean isPageElementsPresent() {

		Boolean bTempResult = true;

		if (!isPortletElementsPresent()) {
			bTempResult = false;
		}
		if (!isElementPresent(pages.csPortlet.by_dobField)) {
			System.out.println("DOB element not found.");
			bTempResult = false;
		}
		if (!isElementPresent(pages.csPortlet.by_genderDropdown)) {
			System.out.println("Gender element not found.");
			bTempResult = false;
		}
		if (!isElementPresent(pages.csPortlet.by_submitButton)) {
			System.out.println("Submit element not found.");
			bTempResult = false;
		}
		if (!isElementPresent(pages.csPortlet.by_clearButton)) {
			System.out.println("ClearS element not found.");
			bTempResult = false;
		}
		if (!isElementPresent(pages.csLabResultsPage.by_poweredByEHealthOntarioPortlet)) {
			System.out.println("Powered By eHealth Ontario portlet not found.");
			bTempResult = false;
		} else {
			Assert.assertEquals(
					pages.csLabResultsPage.POWERED_BY_EHEALTH_ONTARIO,
					pages.csLabResultsPage.poweredByEHealthOntarioPortlet(driver)
							.getText());
		}
		if (!isElementPresent(pages.csLabResultsPage.by_poweredByEHealthOntarioImage)) {
			System.out.println("PoweredByEHealthOntario image not found.");
			bTempResult = false;
		}

		if (!isElementPresent(pages.csLabResultsPage.by_patientLabResultsPortlet)) {
			System.out.println("Patient Lab Results portlet not found.");
			bTempResult = false;
		}
		if (!isElementPresent(pages.csLabResultsPage.by_odbClaimsHistoryPortlet)) {
			System.out.println("ODB Claims History portlet not found.");
			bTempResult = false;
		}

		if (bTempResult == true) {
			return true;
		} else {
			return false;
		}
	}

}
